package com.qiantu.api.db;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.qiantu.api.db.database.LocalDataBase;
import com.qiantu.api.db.greendao.DeviceBindGroupBeanDao;
import com.qiantu.api.entity.DeviceBindGroupBean;

import java.util.List;

public class LLDeviceGroupDBManager {
    private final String TAG = "LLDeviceGroupDBManager";
    private volatile static LLDeviceGroupDBManager instance = null;
    private final static Object object = new Object();
    private final DeviceBindGroupBeanDao mDeviceBindGroupBeanDao;
    private Context context;

    private LLDeviceGroupDBManager(Context context) {
        this.context = context;
        mDeviceBindGroupBeanDao = LocalDataBase.getInstance().getDaoSession(context).getDeviceBindGroupBeanDao();
    }

    public static LLDeviceGroupDBManager getInstance(Context context) {
        if (instance == null) {
            synchronized (object) {
                if (instance == null) {
                    instance = new LLDeviceGroupDBManager(context.getApplicationContext());
                }
            }
        }
        return instance;
    }

    public void clearFromDB() {
        mDeviceBindGroupBeanDao.deleteAll();
    }

    /**
     * 从服务器更新设备数据
     *
     * @param deviceBindGroupBeanList
     */
    public void refreshAllDevicesTx(List<DeviceBindGroupBean> deviceBindGroupBeanList, HandleListener listener) {
        LocalDataBase.getInstance().getDaoSession(context).runInTx(() -> {
            clearFromDB();
            if(deviceBindGroupBeanList == null){
                return;
            }
            Log.i("lzxzx", "refreshAllDevicesTx:" + deviceBindGroupBeanList.size());
            for (DeviceBindGroupBean deviceBindGroupBean : deviceBindGroupBeanList) {
                mDeviceBindGroupBeanDao.save(deviceBindGroupBean);
            }
        });
    }

    public void addSingleGroupData(DeviceBindGroupBean deviceBindGroupBean) {
        LocalDataBase.getInstance().getDaoSession(context).runInTx(() -> {
            mDeviceBindGroupBeanDao.save(deviceBindGroupBean);
        });
    }

    public List<DeviceBindGroupBean> getDeviceGroupsByDeviceGroupSerialNo(List<String> deviceGroupSerialNo) {
        return mDeviceBindGroupBeanDao.queryBuilder()
                .where(DeviceBindGroupBeanDao.Properties.DeviceGroupSerialNo.in(deviceGroupSerialNo))
                .list();
    }

    public List<DeviceBindGroupBean> getDeviceGroupsByDeviceSerialNo(String deviceSerialNo) {
        return mDeviceBindGroupBeanDao.queryBuilder()
                .where(DeviceBindGroupBeanDao.Properties.DeviceSerialNo.in(deviceSerialNo))
                .list();
    }

    /**
     * 保存或者更新群组设备
     *
     * @param deviceList
     */
    public void addBatch(List<DeviceBindGroupBean> deviceList) {
        LocalDataBase.getInstance().getDaoSession(context).runInTx(() -> {
            for (DeviceBindGroupBean deviceBindGroupBean : deviceList) {
                mDeviceBindGroupBeanDao.save(deviceBindGroupBean);
            }
        });
    }

    /**
     * 删除设备by controllerId
     *
     * @param groupSerialNo
     */
    public boolean removeByGroupSerialNo(String groupSerialNo) {
        if (TextUtils.isEmpty(groupSerialNo))
            return false;
        List<DeviceBindGroupBean> devices = mDeviceBindGroupBeanDao.queryBuilder()
                .where(DeviceBindGroupBeanDao.Properties.DeviceGroupSerialNo.eq(groupSerialNo))
                .list();
        if (devices != null && devices.size() > 0) {
            for (DeviceBindGroupBean device : devices) {
                mDeviceBindGroupBeanDao.delete(device);
            }
            return true;
        }
        return false;
    }

    public boolean removeDeviceInGroup(String groupSerialNo,String deviceSerialNo) {
        if (TextUtils.isEmpty(groupSerialNo))
            return false;
        List<DeviceBindGroupBean> devices = mDeviceBindGroupBeanDao.queryBuilder()
                .where(DeviceBindGroupBeanDao.Properties.DeviceGroupSerialNo.eq(groupSerialNo),DeviceBindGroupBeanDao.Properties.DeviceSerialNo.eq(deviceSerialNo))
                .list();
        if (devices != null && devices.size() > 0) {
            for (DeviceBindGroupBean device : devices) {
                mDeviceBindGroupBeanDao.delete(device);
            }
            return true;
        }
        return false;
    }
}


